<template>
	<div class="shopcart-container">
		<div v-if="cartList.length > 0">
			<!-- 全选 清空 -->
			<div class="cart_header">
				<!-- 全选 -->
				<div class="cart_title">
					<van-checkbox checked-color="#000000" icon-size="18" v-model="isCheckedAll" @click="onSelectAll">全选
					</van-checkbox>
				</div>
				<!-- 清空 -->
				<div class="cart_clear" @click="onClickDeleteAll">
					<van-icon name="delete" />
					<span>清空</span>
				</div>
			</div>

			<!-- 商品列表区域 -->
			<div style="background-color: white;">
				<van-row v-for="(item, i) in cartList" :key="i" type="flex" align="center">
					<van-col span="2">
						<!-- 复选框 -->
						<van-checkbox v-model="item.isChecked" @change="onCheckedChange(item)" />
					</van-col>

					<van-col span="5">
						<!-- 中间商品图片 -->
						<van-icon size="80" :name="APIDomainName + item.pic"></van-icon>
					</van-col>

					<van-col span="17">
						<van-row class="van-multi-ellipsis--l2 title">{{ item.title }}</van-row>
						<van-row type="flex" align="center">

							<van-swipe-cell style="width: 100%;">
								<van-row justify="space-between">
									<span class="price">¥{{ item.price }}</span>
									<van-stepper v-model="item.count" @change="onCountChanged(item)" integer />
								</van-row>

								<template #right>
									<van-button text="删除" size="small" @click.prevent="onClickDeleteCartItem(item.id)" />
								</template>
							</van-swipe-cell>

						</van-row>
					</van-col>
				</van-row>
			</div>

			<!-- 商品结算区域 -->
			<van-cell title="商品结算" value="总计不含运费"></van-cell>
			<van-cell>
				<template #title>
					已勾选商品<span class="red">{{ getCheckedCount }}</span>
					件，总价
					<span class="red">¥{{ getCheckedAmount }}</span>
				</template>
			</van-cell>

			<!-- 结算按钮 -->
			<van-button type="primary" size="large" @click="onClickCreateOrder">去结算</van-button>

		</div>
		<div v-else>
			<van-image :src="shopcartEmpty">

			</van-image>
		</div>
	</div>

</template>

<script>
	import {
		getCartListAPI,
		updateCartCountAPI,
		updateCartCheckedAPI,
		deleteCartItemAPI,
		updateCartCheckedAllAPI,
		deleteCartAllAPI
	} from "@/api/cart.js";

	import { showConfirmDialog } from 'vant';
	import shopcartEmpty from "@/assets/images/shopcart_empty.png"
	export default {
		data() {
			return {
				cartList: [],
				isCheckedAll: false, //是否全选
				shopcartEmpty: shopcartEmpty,
			};
		},

		created() {
			this.loadCartList();
		},

		computed: {
			//计算勾选记录总数
			getCheckedCount() {
				let sum = 0;
				this.cartList.forEach((item) => {
					if (item.isChecked) sum += 1;
				});
				return sum;
			},

			//计算是否全部勾选
			getCheckedAll() {
				return this.cartList.length === this.getCheckedCount ? true : false;
			},

			//计算总金额
			getCheckedAmount() {
				let amount = 0;
				this.cartList.forEach((item) => {
					if (item.isChecked) amount += item.price * item.count;
				});
				return amount;
			},

		},

		methods: {
			//获取用户购物车数据
			loadCartList() {
				this.cartList = []; //清空
				getCartListAPI().then(result => {
					console.log(result)
					this.cartList = result.data;
					this.isCheckedAll = this.getCheckedAll; // 重置isCheckedAll属性值
				}).catch((err) => { console.log(err) });
			},

			//触发更改购物商品数量
			onCountChanged(item) {
				let params = {
					id: item.id,
					count: item.count
				};

				updateCartCountAPI(params).then(() => {

				});
			},

			//触发更改购物商品选中状态
			onCheckedChange(item) {
				let id = item.id;
				let isChecked = item.isChecked == true ? 1 : 0;

				let params = { id, isChecked };
				updateCartCheckedAPI(params).then(() => {
					this.isCheckedAll = this.getCheckedAll;
				}).catch(() => {});
			},

			//触发删除购物商品
			onClickDeleteCartItem(id) {
				let ids = id;
				deleteCartItemAPI(ids).then(() => {
					this.loadCartList();
				});
			},

			//触发全选/全不选
			onSelectAll() {
				let isChecked = this.isCheckedAll == true ? 1 : 0;
				updateCartCheckedAllAPI(isChecked).then(() => {
					this.loadCartList();
				});
			},

			//触发清空
			onClickDeleteAll() {
				showConfirmDialog({
					message: '客官，您确定清空购物车吗?'
				}).then(() => {
					deleteCartAllAPI().then(() => {
						this.loadCartList();
					});
				}).catch(() => {})
			},

			//触发创建订单
			onClickCreateOrder() {
				if (this.cartList.length === 0) {
					this.$toast("您的购物车为空");
					return;
				}
console.log("aaaaaaa")
				//要确保购物车数据和后端数据库的是一致的 .....
				this.$router.push({
					name: "orderCreate"
				});

			},

		},
	};
</script>



<style lang="less" scoped>
	.shopcart-container {
		padding-left: 10px;
		padding-right: 10px;

		.cart_header {
			padding: 10px;
			margin: 10px 0;
			height: 20px;
			background-color: white;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}

	.van-checkbox {
		justify-content: center;
	}

	.title {
		padding: 5px 5px;
		padding-left: 20px;
		font-size: 15px;
		line-height: 20px;
	}

	.price {
		padding-left: 20px;
		text-align: left;
		font-size: 16px;
		font-weight: bold;
		color: #c5ae41;
		display: flex;
		align-items: center;
	}

	.opration {
		padding-right: 10px;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.red {
		color: #c5ae41;
		font-weight: bold;
		font-size: 16px;
	}
</style>